package club.twelveblog.tblog.dao;

import club.twelveblog.tblog.entity.BlogInfo;
import club.twelveblog.tblog.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface BlogInfoDao extends JpaRepository<BlogInfo, Long>, PagingAndSortingRepository<BlogInfo, Long> {


    //SELECT b.blog_id,b.blog_content,b.blog_preface,b.blog_status,b.blog_sub_url,b.blog_title,b.blog_views,b.enable_comment,b.is_deleted,b.create_time,b.update_time,c.category_name FROM tb_bloginfo b,tb_category c WHERE b.c_id=c.category_id

    //
//    @Query(value = "SELECT * FROM tb_blogInfo WHERE blog_sub_url=?1",nativeQuery = true)
//    BlogInfo findByUrl(String url);
    //b.blog_id,b.blog_title,b.blog_preface,b.blog_views,b.u_id,b.blog_content,u.user_nick_name,u.author_img
//
//    @Query(value = "SELECT new club.twelveblog.tblog.common.vo.BlogInfoVo(b.blog_id,b.blog_title,b.blog_preface,b.blog_views,b.u_id,b.blog_content,u.user_nick_name,u.author_img)" +
//            "FROM tb_bloginfo b,tb_category c,tb_user u " +
//            "WHERE b.c_id=c.category_id " +
//            "AND b.u_id=u.user_id " +
//            "AND c.category_name=?1",nativeQuery = true)
//    Page<BlogInfo> findBlogByCategory(String category,Pageable pageable);
    Page<BlogInfo> findByUser(User user, Pageable pageable);

    @Query(value = "SELECT b FROM BlogInfo b WHERE b.is_deleted=0")
    Page<List<BlogInfo>> queryBlogsNoDel(Pageable pageable);

    //查找当前用户下所有的博客文章(未删除状态)
    @Query(value ="SELECT * FROM tb_bloginfo WHERE is_deleted=0 AND u_id=?1",nativeQuery = true)
    Page<List<BlogInfo>> findMyBlogInfo(Long userId,Pageable pageable);


    @Query(value = "SELECT * FROM tb_bloginfo WHERE c_id=?1",nativeQuery = true)
    Page<List<BlogInfo>> findBlogInfoByCid(Long category_id,Pageable pageable);
}
